package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EquipAccepFormMap;
import com.iemes.entity.EquipApplyFormMap;
import com.iemes.entity.EquipFileFormMap;
import com.iemes.entity.MetalsBomFormMap;
import com.iemes.entity.ReceiveFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.DepartmentMapper;
import com.iemes.mapper.equip.EquipAccepMapper;
import com.iemes.mapper.equip.EquipFileMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("equip")
@Controller
public class EquipAccepController extends BaseController {
	@Inject
	private EquipAccepMapper accepMapper;
	@Inject
	private DepartmentMapper departmentMapper;
	@Inject
	private EquipFileMapper filemapper;

	@RequestMapping(value = "equip_acceptance", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<EquipAccepFormMap> findByWhere = accepMapper
				.findByWhere(new EquipAccepFormMap());
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("equipbom", findByWhere);
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/equip/accep/equipbom_list";
	}

	@ResponseBody
	@RequestMapping("equip_accepByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		EquipAccepFormMap equipAccepFormMap = getFormMap(EquipAccepFormMap.class);
		if (equipAccepFormMap.get("states")==null) {
			equipAccepFormMap.put("states", 1);
		}
		List<Map<String, Object>> equipAccp = accepMapper
				.getEquipAccp(equipAccepFormMap);
		try {
			LuogicPaging(pageNow, pageSize, equipAccp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;

	}

	@RequestMapping("accep_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		DepartmentForMap departmentForMap = new DepartmentForMap();
		List<DepartmentForMap> findByAll = departmentMapper
				.findByNames(departmentForMap);
		EquipAccepFormMap equipAccp = accepMapper.findbyFrist("id", id,
				EquipAccepFormMap.class);
		model.addAttribute("equipAccp", equipAccp);
		model.addAttribute("findByAll", findByAll);
		return Common.BACKGROUND_PATH + "/equip/accep/equipbom_edit";
	}

	@ResponseBody
	@RequestMapping(value = "accep_editEntity")
	@SystemLog(module = "设备验收单", methods = "验收单-修改设备验收单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		EquipAccepFormMap equipAccepFormMap = getFormMap(EquipAccepFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		equipAccepFormMap.put("date", sdf.format(date));
		equipAccepFormMap.put("fillPeopleId", userFormMap.get("id"));
		accepMapper.editEntity(equipAccepFormMap);
		return "success";
	}

	@ResponseBody
	@Transactional(readOnly = false)
	@RequestMapping(value = "accep_qualifiedEntity")
	@SystemLog(module = "设备验收单", methods = "设备-合格验收单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String qualifiedEntity() throws Exception {
		EquipFileFormMap equipFileFormMap = getFormMap(EquipFileFormMap.class);
		EquipAccepFormMap equipAccepFormMap = getFormMap(EquipAccepFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for (int i = 0; i < str.length; i++) {
			equipFileFormMap.put("status", 1);
			equipFileFormMap.put("equipmentCheckId", str[i]);
			equipFileFormMap.put("fillPeopleId", userFormMap.get("id"));
			equipAccepFormMap.put("judgeResult", 1);
			equipAccepFormMap.put("fillPeopleId", userFormMap.get("id"));
			equipAccepFormMap.put("id", str[i]);
			// 合格之后向添加设备台账表
			filemapper.addEntity(equipFileFormMap);
			// 修改判断结果
			accepMapper.editEntity(equipAccepFormMap);
			equipFileFormMap.clear();
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "accep_deleteEntity")
	@SystemLog(module = "设备验收单", methods = "设备-删除验收单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		EquipAccepFormMap equipAccepFormMap = getFormMap(EquipAccepFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		 String realPath = request.getSession(false).getServletContext().getRealPath("/");
		for (int i = 0; i < str.length; i++) {
			equipAccepFormMap.put("status", 2);
			equipAccepFormMap.put("id", str[i]);
			equipAccepFormMap.put("fillPeopleId", userFormMap.get("id"));
			accepMapper.editEntity(equipAccepFormMap);

		}

		return "success";
	}

}
